package com.zsck.zsgy.ui.main.fragment

import android.os.Bundle
import android.text.TextUtils
import android.view.View
import com.zsck.zsgy.R
import com.zsck.zsgy.base.BaseFragment
import com.zsck.zsgy.databinding.FragmentMineLayoutBinding
import com.zsck.zsgy.ui.main.bean.CoverFile
import com.zsck.zsgy.ui.main.fragment.mine.bean.MineViewModel
import com.zsck.zsgy.util.Constants
import com.zsck.zsgy.util.GlideUtils
import com.zsck.zsgy.util.PrefserHelper

class MineFragment : BaseFragment<MineViewModel, FragmentMineLayoutBinding>() {
    companion object {
        fun newInstance() = MineFragment()
    }

    override fun initView(savedInstanceState: Bundle?) {


    }

    override fun layoutId(): Int {
        return R.layout.fragment_mine_layout
    }


    override fun lazyLoad() {

        if(TextUtils.isEmpty(PrefserHelper.getUserKey())){
            binding.ivHead.setImageResource(R.mipmap.default_head)
            binding.tvName.text=getString(R.string.login_signup)
            binding.tvPhone.visibility= View.GONE
            binding.llLoginSignup.isEnabled=true
            binding.llLoginSignup.isClickable=true
        }else{
            getUserInfo()
        }
        getMessage()

        viewModel.getConfigList()
        viewModel.mineConfigModel.observe(this, {

        })
    }

    private fun getMessage() {
        viewModel.getMessage()
        viewModel.messageModel.observe(this, {

        })
    }

    fun getUserInfo(){
        viewModel.getUserInfo()
        viewModel.userInfoModel.observe(this, {
            binding.ivHead.setImageResource(R.mipmap.default_head)
            binding.tvName.text = it.nickName
            binding.tvPhone.text = it.phone
            binding.tvPhone.visibility = View.VISIBLE
            binding.llLoginSignup.isEnabled = false
            binding.llLoginSignup.isClickable = false
            val file: CoverFile = it.getHeadPortraitFile()
            if (file == null && !TextUtils.isEmpty(it.getPhotoUrl())) {
                GlideUtils.displayWithCircle(it.getPhotoUrl(), binding.ivHead, activity)
            } else if (file != null) {
                GlideUtils.displayWithCircle(
                    Constants.IMG_HOST + file.getFileId(),
                    binding.ivHead,
                    mContext
                )
            }

        })
    }
}